package com.lyl.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.commonutils.R;
import com.lyl.serviceedu.entity.Course;
import com.lyl.serviceedu.entity.EduTeacher;
import com.lyl.serviceedu.entity.vo.CourseInfoVo;
import com.lyl.serviceedu.entity.vo.CoursePublishVo;
import com.lyl.serviceedu.query.CourseQuery;
import com.lyl.serviceedu.service.CourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-06-11
 */
@RestController
@RequestMapping("/serviceedu/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable Long id){

        boolean result = courseService.removeCourse(id);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }
    @ApiOperation(value = "分页课程列表")
    @PostMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    CourseQuery courseQuery){

        Page<Course> pageParam = new Page<>(page, limit);

        courseService.pageQuery(pageParam, courseQuery);
        List<Course> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }



    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后课程id，为了后面添加大纲使用
        Long id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    @PutMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后课程id，为了后面添加大纲使用
        boolean isok = courseService.updateInfoVo(courseInfoVo);
        if(isok) {
            return R.ok().message(" 修改成功");
        }else{
            return  R.error().message("修改失败");
        }
    }




    //添加课程基本信息的方法
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        //返回添加之后课程id，为了后面添加大纲使用
        CourseInfoVo courseInfo=  courseService.getByCourseId(id);
        return R.ok().data("courseInfo",courseInfo);
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable Long id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable Long id) {
        Course eduCourse = new Course();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        courseService.updateById(eduCourse);
        return R.ok();
    }


}

